import json
# 支持上级目录导入
import os,sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from read_filedata import read_shipdata, write_actiondata

AIS_STOPTIME = None # ais信号关闭时间
with open('config.json','r', encoding='utf-8') as file:
    config = json.load(file)
    print(f'config.signal: {config["signal"]}')
    AIS_STOPTIME = config['signal']['stopAisTime']
signal_latest_time = {} # ais信号最新时间
signal_close_time = {} # ais信号关闭时间，避免重复判断

def check_signal_ship(data_input, now_timestamp):
    data_output = []
    # 补充最新时间记录
    for data in data_input:
        track_id = data['trackId']
        first_data = data['trackData'][0]
        if signal_latest_time.get(track_id, None) is None:
            signal_latest_time[track_id] = first_data['timestamp']
    # 遍历所有轨迹点
    for data in data_input:
        track_id = data['trackId']
        prev_time = signal_latest_time[track_id]
        for sub_data in data['trackData']:
            sub_time = sub_data['timestamp']
            if sub_time - prev_time >= AIS_STOPTIME:
                # print('offset-time', sub_time - prev_time)
                # 超出时间间隔，则判断上个时间有信号关闭行为
                if signal_close_time.get(track_id, 0) != prev_time:
                    data_output.append({
                        'trackId': track_id,
                        'actionKey': '1A',
                        'startTime': prev_time,
                        'endTime': prev_time,
                        'properties': f'{sub_data["longitude"]};{sub_data["latitude"]}'
                    })
                    signal_close_time[track_id] = prev_time
                # 超出时间间隔，且出现信号，则判断当前时间有信号打开行为
                data_output.append({
                    'trackId': track_id,
                    'actionKey': '1B',
                    'startTime': sub_time,
                    'endTime': sub_time,
                    'properties': f'{sub_data["longitude"]};{sub_data["latitude"]}'
                })
            prev_time = sub_time
        signal_latest_time[track_id] = prev_time
        # 比较最新时间
        if now_timestamp - prev_time >= AIS_STOPTIME:
            # 超出时间间隔，则判断上个时间有信号关闭行为
            if signal_close_time.get(track_id, 0) != prev_time:
                data_output.append({
                    'trackId': track_id,
                    'actionKey': '1A',
                    'startTime': prev_time,
                    'endTime': prev_time,
                    'properties': f'{sub_data["longitude"]};{sub_data["latitude"]}'
                })
                signal_close_time[track_id] = prev_time
    # print(f'\ncheck_signal_ship.length: {len(data_output)}')
    return data_output

if __name__ == '__main__':
    # 此处可自行调试
    # 读取文件生成飞机实时状态类输入数据
    ship_list, now_timestamp = read_shipdata()
    print(f'read_shipdata: {len(ship_list)},{now_timestamp}')
    print(f'read_shipdata[0]: {ship_list[0]}')
    # 运行算法程序
    action_signal = check_signal_ship(ship_list, now_timestamp)
    print(f'\check_signal_ship: {action_signal}')
    # 行为写入文件
    write_actiondata(action_signal, True)

